package com.example.compreshensiveshop.controller;

import com.example.compreshensiveshop.dto.*;
import com.example.compreshensiveshop.po.SkuPO;
import com.example.compreshensiveshop.service.ProductService;
import com.example.compreshensiveshop.service.SkuService;
import com.example.compreshensiveshop.utils.QiniuKodoUtil;
import com.example.compreshensiveshop.vo.*;
import com.github.pagehelper.PageInfo;
import org.apache.tomcat.util.http.fileupload.FileUpload;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Queue;

@RestController
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private ProductService productService;
    @Autowired
    private QiniuKodoUtil qiniuKodoUtil;

    /**
     * 添加商品SKU接口
     */
    @PostMapping("/addSku")
    public void addProductSku(@RequestBody SkuCreateDTO skuCreateDTO) {
        productService.addProductSku(skuCreateDTO);
    }

    /**
     * 查询商品SKU接口
     */
    @GetMapping("/queryByPId/{productId}")
    public List<SkuListVO> queryByPId(@PathVariable Long productId) {
        return productService.queryByPId(productId);
    }

    /**
     * 修改商品SKU接口
     */
    @PutMapping("/update")
    public void updateProductSku(@RequestBody SkuUpdateDTO skuUpdateDTO) {
        productService.updateProductSku(skuUpdateDTO);
    }

    /**
     * 修改商品接口
     */
    @PutMapping("/updateProduct")
    public void updateProduct(@RequestBody ProductUpdateDTO productUpdateDTO) {
        productService.updateProduct(productUpdateDTO);
    }

    /**
     * 修改商品状态接口
     */
    @PutMapping("/updateStatus")
    public void updateStatus(@RequestParam Long id, @RequestParam Integer status) {
        productService.updateStatus(id, status);
    }

    /**
     * 添加商品接口
     */
    @PostMapping("/addProduct")
    public Long addProduct(@RequestBody ProductCreateDTO productCreateDTO) {
        return productService.addProduct(productCreateDTO);
    }

    /**
     * 查询商品信息接口
     */
    @GetMapping("/queryMessageById/{id}")
    public ProductMessageVO queryMessageById(@PathVariable Long id) {
        return productService.queryMessageById(id);
    }

    /**
     * 分页查询接口
     */

    @GetMapping("/query")
    public PageResultVO<ProductVO> queryProducts(
            @RequestParam(required = false) Long categoryId,
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) Long sellerId,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) Integer status) {

        ProductQueryDTO queryDTO = new ProductQueryDTO();
        queryDTO.setCategoryId(categoryId);
        queryDTO.setKeyword(keyword);
        queryDTO.setSellerId(sellerId);
        queryDTO.setPage(page);
        queryDTO.setSize(size);
        queryDTO.setStatus(status);

        return productService.queryProductsByCondition(queryDTO);
    }

    /**
     * 上传图片接口
     */
    @PostMapping("/uploadImages")
    public String uploadImages(@RequestBody MultipartFile file) {
        return qiniuKodoUtil.upload(file);
    }

    /**
     * 删除图片接口
     */
    @DeleteMapping("/deleteImages")
    public void deleteImages(@RequestParam Long id) {
        productService.deleteImages(id);
    }

    /**
     * 获取商品主图接口
     */
    // 增加商品浏览量接口
    @PostMapping("/incrementViewCount/{productId}")
    public void incrementViewCount(@PathVariable Long productId) {
        productService.incrementViewCount(productId);
    }


}